/**
 * @author Praveen Krupakaran (A0092625)
 */

package sg.edu.nus.iss.team4ft.manager;

import java.io.EOFException;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import sg.edu.nus.iss.team4ft.service.Complaint;
import sg.edu.nus.iss.team4ft.service.CustomerAccount;
import sg.edu.nus.iss.team4ft.util.AppConstant;
import sg.edu.nus.iss.team4ft.util.IOService;

public class ComplaintManager {
	private int numberOfComplaints = 0; 
	private Complaint complaint;
	private String complaintID;
	private ArrayList<Complaint> listOfComplaints ;
	private ArrayList<Complaint> listOfComplaintsforacc ;
	
	@SuppressWarnings("unchecked")
	public ComplaintManager() throws EOFException{
		try{
		listOfComplaints = new ArrayList<Complaint>();		
		new CustomerAccount();
		new CustomerAccountManager();
		listOfComplaints = (ArrayList<Complaint>)IOService.readObjFromFile(AppConstant.FILE_CMPLNTS);
		if(listOfComplaints == null){
			listOfComplaints = new ArrayList<Complaint>();	
		}
		}catch(Exception e){
			System.out.println("Complaints File has no Complaints registered yet");
			listOfComplaints = new ArrayList<Complaint>();
		}
	}	
	
	public ArrayList<Complaint> getListOfComplaints() {
		return listOfComplaints;
	}
	
	public ArrayList<Complaint> getListOfComplaintsforAccountNum(String accountNumber) throws CloneNotSupportedException {
		
		listOfComplaintsforacc = new ArrayList<Complaint>();
		for(int index = 0 ; index < listOfComplaints.size(); index++){
			complaint = listOfComplaints.get(index);
			if(complaint.getAccountNumber().compareTo(accountNumber)==0){
				listOfComplaintsforacc.add(complaint);
			}
		}
		
		return listOfComplaintsforacc;
	}



	public boolean updateComplaint(String complaintID ,String desription , int status, String accountNumber, String  plan) throws CloneNotSupportedException, IOException{
		
		for(int index = 0 ; index < listOfComplaints.size(); index++){
			Complaint comp = listOfComplaints.get(index); 
			if(complaintID.compareTo(comp.getComplaintID())==0){
				listOfComplaints.remove(index);
				break;
			}
		}
		complaint = new Complaint(complaintID, desription, status, accountNumber, plan, new Date());
		listOfComplaints.add(complaint);
		IOService.saveObjToFile(listOfComplaints, AppConstant.FILE_CMPLNTS);
		return true;
	}
	
	
	
	//This method finds the CustomerAccount to which the complaint has to be added
	//and adds new Complaint to the corresponding CustomerAccount. This method also returns the generated ComplaintID
	public void addComplaint(String complaintID ,String description , String accountNumber, String plan) throws CloneNotSupportedException, NullPointerException, IOException, ParseException, Exception {		
		
		int status = 0;
				
				// Creating New Complaint using the complaintID and description from the user
				complaint = new Complaint(complaintID, description, status, accountNumber, plan, new Date());
				
				//Add the new Complaint to the corresponding CustomerAccount
				listOfComplaints.add(complaint);
				
				IOService.saveObjToFile(listOfComplaints, AppConstant.FILE_CMPLNTS);
	}
	
//This Method is used only for testing updateComplaint() method. This method will not be used anywhere in the system	
	public void setComplaint(Complaint complaint) {
		this.complaint = complaint;
	}

//	This Method is used only for testing getListOfComplaints() method. This method will not be used anywhere in the system
	public void setListOfComplaints(ArrayList<Complaint> listOfComplaints) {
		this.listOfComplaints = listOfComplaints;
	}

	public String generateComplaintID() throws CloneNotSupportedException{
		
		if(listOfComplaints == null){
			numberOfComplaints = 0;
		}else{			
			numberOfComplaints = listOfComplaints.size();
		}
		complaintID = "COM"+ ((Integer)(++numberOfComplaints)).toString();
		return complaintID;		
	}

	public int getNumberOfComplaints() {
		return numberOfComplaints;
	}

	public String getComplaintID() {
		// TODO Auto-generated method stub
		return complaintID;
	}
	
	public Complaint getComplaint(String accNumber,String complaintID) throws CloneNotSupportedException{
		
		ArrayList<Complaint> tempListOfComplaints = getListOfComplaintsforAccountNum(accNumber);
		for(int index = 0 ; index < tempListOfComplaints.size(); index++){
			Complaint comp = tempListOfComplaints.get(index); 
			if(complaintID.compareTo(comp.getComplaintID())==0){
				complaint = comp;
				return complaint;
			}
		}
		
		
		return null;
	}
	
public Complaint getComplaint(String complaintID) throws CloneNotSupportedException{
		
		
		for(int index = 0 ; index < listOfComplaints.size(); index++){
			Complaint comp = listOfComplaints.get(index);
			if(complaintID.compareTo(comp.getComplaintID())==0){
				complaint = comp;
				return complaint;
			}
		}		
		return null;
	}	
}

